//头像
window.onload = function () {
    var input = document.querySelector('input[type=file]');
    input.onchange = function (e) {
        if ($('#fileicon').val() != '') {
            document.getElementById('room-icon').src = getFileUrl();
        }
    }
}

//获取本地图片地址
function getFileUrl(fileId) {
    var url;
    var file = document.getElementById('fileicon');
    var agent = navigator.userAgent;
    if (agent.indexOf("MSIE") >= 1) {
        url = file.value;
    } else if (
        agent.indexOf("Firefox") > 0) {
        url = window.URL.createObjectURL(file.files.item(0));
    } else if (
        agent.indexOf("Chrome") > 0) {
        url = window.URL.createObjectURL(file.files.item(0));
    }
    return url;
}

$('#hold').click(function () {
    fd = new FormData(document.getElementById('form'));
    $.ajax({
        url: '/user/editroom',
        type: 'POST',
        timeout: 3000,
        dataType: 'JSON',
        contentType: 'application/x-www-form-urlencoded;charset=utf-8',
        data: fd,
        contentType: false, //不设置内容类型
        processData: false, //不处理数据
        success: function (data) {
            if (data.code == 200) {
                layer.msg(data.msg, {
                    time: 1000 //1秒关闭（如果不配置，默认是3秒）
                }, function () {
                    parent.location.reload();
                });
            } else {
                layer.msg(data.msg);
            }
        },
        error: function (XMLResponse) {
            //异常捕获
            layer.msg(XMLResponse.responseText);
        }
    });
});

$('#hold2').click(function () {
    var id = document.getElementById('room_id').value;
    var fd = new FormData();
    fd.append('id', id);
    $.ajax({
        url: '/user/delroom',
        type: 'POST',
        timeout: 3000,
        dataType: 'JSON',
        contentType: 'application/x-www-form-urlencoded;charset=utf-8',
        data: fd,
        contentType: false, //不设置内容类型
        processData: false, //不处理数据
        success: function (data) {
            if (data.code == 200) {
                layer.msg(data.msg, {
                    time: 1000 //1秒关闭（如果不配置，默认是3秒）
                }, function () {
                    parent.location.reload();
                });
            } else {
                layer.msg(data.msg);
            }
        },
        error: function (XMLResponse) {
            //异常捕获
            layer.msg(XMLResponse.responseText);
        }
    });
});
